package game.Sprites;

import game.Utils.GraphicsUtils;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import net.sf.image4j.codec.bmp.BMPDecoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;

public class SpriteResources {
    protected static Image flareImg;
    protected static Image shadowImg;
    protected static List<Image> bloodImgs, debryImgs;
    protected static Image bloodStain;
    protected static Image knifeSlash;
    protected static Image plantedBomb;
    protected static Image bombEquipment;
    protected static SpriteSheet shadowSheet;
    protected static SpriteSheet explosionSheet;


    public static void init() throws Exception {
        flareImg =  SwingFXUtils.toFXImage(ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/flare.png")), null);
        shadowImg =  SwingFXUtils.toFXImage(ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/shadow.png")), null);
        bloodStain =  SwingFXUtils.toFXImage(ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/blood.png")), null);
        knifeSlash =  SwingFXUtils.toFXImage(ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/knifeslash.png")), null);
        plantedBomb =  SwingFXUtils.toFXImage(Transparency.loadTransparentBMP(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/Weapons/plantedbomb_d.bmp")), null);
        bombEquipment =  SwingFXUtils.toFXImage(ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/player/bomb_back.png")), null);

        //BufferedImage shadowImg = ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/shadowmap.bmp"));
        //shadowSheet = new SpriteSheet(shadowImg, 2, 9);
        explosionSheet = new SpriteSheet(ImageIO.read(SpriteResources.class.getClassLoader().getResourceAsStream("gfx/explosion.png")), 5, 5);
        loadDebrys();
        loadBlood();
    }

    public static Image getFlareImage(){
        return flareImg;
    }

    public static Image getShadowImage(){
        return shadowImg;
    }

    public static Image getBombEquipmentImage(){
        return bombEquipment;
    }

    public static Image getPlantedBombImage(){
        return plantedBomb;
    }

    public static SpriteSheet getExplosionSheet(){
        return explosionSheet;
    }

    protected static void loadDebrys(){
        debryImgs = new ArrayList<Image>();
        boolean exit = false;
        for(int i = 1; i <= 4 && !exit; i++){
            try {
                Image debryImg = SwingFXUtils.toFXImage(ImageIO.read(
                        SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/gfx/debry" + i + ".png")), null);

                debryImgs.add(debryImg);
            } catch (IOException e) {
                exit = true;
            }
        }
    }
    protected static void loadBlood(){
        bloodImgs = new ArrayList<Image>();
        boolean exit = false;
        for(int i = 1; i <= 8 && !exit; i++){
            try {
                Image bloodImg = SwingFXUtils.toFXImage(ImageIO.read(
                        SpriteResources.class.getClassLoader().getResourceAsStream("Sprites/gfx/blood" + i + ".png")), null);

                bloodImgs.add(bloodImg);
            } catch (IOException e) {
                exit = true;
            }
        }
    }

    protected static Image getBloodImg(){
        int max = bloodImgs.size();
        int rnd = (int)(Math.random() * max); // max not included

        return bloodImgs.get(rnd);
    }

    protected static Image getDebryImg(){
        int max = debryImgs.size();
        int rnd = (int)(Math.random() * max); // max not included

        return debryImgs.get(rnd);
    }
    public static Image getBloodStain(){
        return bloodStain;
    }
    public static Image getKnifeSlash(){
        return knifeSlash;
    }

    public static Image getShadowImg(int i, int j){
        return shadowSheet.get(i, j);
    }
}
